/**
 * @PROJECT 
 * @DATE 2018年7月12日 下午1:55:21
 * @AUTHOR LUYU
 */
package com.kool.tlwp.generator;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

import com.kool.tlwp.bean.IoFieldVo;
import com.kool.tlwp.exception.AppException;
import com.kool.tlwp.generator.context.IoContext;
import com.kool.tlwp.util.DateUtils;
import com.kool.tlwp.util.PropertyUtils;

import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * @DESCRIBE
 * @AUTHOR LUYU
 * @DATE 2018年7月12日 下午1:55:21
 *
 */
public class IoCodeGenerator extends CodeGenerator {

	/**
	 * @param context
	 * @throws AppException
	 */
	public IoCodeGenerator(IoContext context) throws AppException {
		super(context);
	}

	/**
	 * @DESCRIBE
	 * @DATE 2018年7月12日 下午1:55:34
	 *
	 * @throws AppException
	 */
	@Override
	public void generate() throws AppException {
		IoContext ic = (IoContext) context;
		String destDir = ic.getDestDir() + "\\"
				+ DateUtils.getCurrentTimeString(DateUtils.FORMAT_DATE_AND_TIME_WITHOUT_SPLIT);
		String pkg = ic.getPkg();
		String name = ic.getBusinessName();
		String lowName = name.substring(0, 1).toLowerCase() + name.substring(1);
		String ioNames = ic.getIoName();
		String[] arrayIoName = ioNames.split(";");
		for (String ioName : arrayIoName) {
			System.out.println("生成接口："+ioName);
			String ioDir = getIoDir(destDir, pkg, lowName);
			genIo(ic, lowName, ioDir, ioName);
		}

	}

	private void genIo(IoContext ic, String lowName, String ioDir, String ioName) throws AppException {
		try {
			Template template = configuration.getTemplate("IO.ftl");
			template.setOutputEncoding("UTF-8");
			HashMap<String, Object> map = getIoMap(ic, lowName, ioName);
			Writer writer = getIoWriter(ioName, ioDir);
			template.process(map, writer);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
			throw new AppException("操作Template文件错误!", e);
		} catch (TemplateException e) {
			e.printStackTrace();
			throw new AppException("操作Template文件错误!", e);
		} catch (Exception e) {
			e.printStackTrace();
			throw new AppException("工具发生错误!", e);
		}
	}

	private Writer getIoWriter(String ioName, String ioDir) throws IOException {
		String fileName = ioName + "Bean.java";
		String destPath = ioDir + "\\" + fileName;
		Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destPath), "UTF-8"));
		return writer;
	}

	private HashMap<String, Object> getIoMap(IoContext ic, String lowName, String ioName) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("pkg", ic.getPkg());
		map.put("name", lowName);
		map.put("ioName", ioName);
		List<IoFieldVo> list = json2IoVoList(ioName);
		map.put("ioFields", list);
		return map;
	}

	public static void main(String[] args) throws AppException {
		IoContext context = new IoContext();
		Properties prop = null;
		try {
			prop = PropertyUtils.getSysPropFromRoot();
		} catch (IOException e) {
			throw new AppException("", e);
		}
		context.setPkg(prop.getProperty("PACKAGE"));
		context.setTemplateDir(prop.getProperty("TEMPLATE"));
		context.setIoDir(prop.getProperty("IO_DIR"));
		context.setDestDir(prop.getProperty("DIR"));

		context.setUrl(prop.getProperty("URL"));
		context.setDriver(prop.getProperty("DRIVER"));
		context.setName(prop.getProperty("USER"));
		context.setPwd(prop.getProperty("PWD"));

		context.setBusinessName(prop.getProperty("BUSINESS_NAME"));
		context.setIoName(prop.getProperty("IO_NAME"));
		CodeGenerator genr = new IoCodeGenerator(context);
		genr.generate();

	}
}
